<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Search extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model("index_model");
        $this->load->model("search_model");
    }

    function index() {
        $this->right();
        $this->topinfo();
        $this->posts();
        $this->display('search/index.html');
    }
    function right(){
        $where = array();
        $where['hot'] = 1;
        $where['recommend'] = 1;
        $where['status'] = 1;
        $stars = $this->index_model->stars();
        $hotrec = $this->index_model->hot_recommend($where);
        $this->assign('stars', $stars);
        $this->assign('hotrec',$hotrec);
    }
    function topinfo(){
       
        $kw = array();
        $kw = $this->input->get('keyword');
        
       
        $topinfo = $this->search_model->topinfo($kw);
        foreach ($topinfo as $key=>$val){
            $topinfo[$key]['create_time'] = date("Y-m-d H:i",$val['create_time']);
            $topinfo[$key]['detail_url'] = web_url("detail/".$val['id']);
            $topinfo[$key]['space_url'] = web_url("space/".$val['authorid']);
            $topinfo[$key]['cat_url'] = web_url($val['group']."/c/".$val['code']);
            $topinfo[$key]['title'] = str_ireplace("$kw", "<font color=red>$kw</font>", $topinfo[$key]['title']);
        }
        $this->assign('topinfo', $topinfo);
        $this->assign('kw', $kw);
    }
    function posts(){
        $pageIndex = isset($_GET['pageIndex']) ? $_GET['pageIndex'] : 0;
        $records_per_page = 10;
        $kw = $this->input->get('keyword');
        //$kw = iconv("GB2312","UTF-8",urldecode($kw));
        
        //$postcount = $this->search_model->postcount($kw);
        
        $list = $this->search_model->posts($kw,$pageIndex * $records_per_page,$records_per_page);
        foreach ($list as $key=>$val){
            $list[$key]['create_time'] = date("Y-m-d H:i",$val['create_time']);
            $list[$key]['detail_url'] = web_url("detail/".$val['id']);
            $list[$key]['space_url'] = web_url("space/".$val['authorid']);
            $list[$key]['cat_url'] = web_url($val['group']."/c/".$val['code']);
            $list[$key]['title'] = str_ireplace("$kw", "<font color=red>$kw</font>", $list[$key]['title']);
        }
       $this->assign('searchinfo', $list);
    }
}
